package ldh.im.fxgui.handle;

import ldh.im.ImConfig;
import ldh.im.dto.business.av.*;
import ldh.im.dto.business.item.ChatUserListItem;
import ldh.im.dto.business.user.AvLogin;
import ldh.im.dto.business.user.LoginResp;
import ldh.im.dto.enums.ByteType;
import ldh.im.exception.BusinessException;
import ldh.im.fxbase.rpc.service.RpcAvService;
import ldh.im.fxbase.util.RpcUtil;
import ldh.rpc.CodecType;
import ldh.rpc.TerminalType;
import ldh.rpc.cache.CacheService;
import ldh.rpc.client.RpcClientBootstrap;
import ldh.rpc.client.RpcClientService;
import ldh.rpc.dto.CodecReq;
import ldh.rpc.exception.RpcException;

import java.util.HashSet;
import java.util.Set;

public class AvHandler {

    private RpcClientBootstrap rpcClientBootstrap;

    public AvHandler(RpcClientBootstrap rpcClientBootstrap) {
        this.rpcClientBootstrap = rpcClientBootstrap;
    }

    public LoginResp login() throws RpcException {
        CacheService cacheService = RpcUtil.getRpcBootstrap().getRpcConfig().getCacheService();
        Object userId = cacheService.getCache(ImConfig.LOGIN_USER_ID);
        Object tenantCode = cacheService.getCache(ImConfig.TENANT_ID);
        Object authToken = cacheService.getCache(ImConfig.AUTH_TOKEN);
        if (userId != null) {
            CodecType codecType = RpcUtil.getRpcBootstrap().getRpcConfig().getClientCodecType();
            cacheService.setCache(ImConfig.CODEC_TYPE, codecType, null);
            RpcClientService rpcClientService = rpcClientBootstrap.getRpcConfig().getClientBean(RpcClientService.class);
            rpcClientService.codecType(new CodecReq(codecType));

            AvLogin avLogin = new AvLogin();
            avLogin.setTerminalType(TerminalType.pc);
            avLogin.setTenantCode(tenantCode.toString());
            avLogin.setAuthToken(authToken.toString());
            RpcAvService rpcAvService = rpcClientBootstrap.getRpcConfig().getClientBean(RpcAvService.class);
            LoginResp loginResp = rpcAvService.login(avLogin);
            return loginResp;
        }
        throw new BusinessException("userId为空");
    }

    public String createGroup(String groupName, Set<String> userSet) throws RpcException {
        AvGroupCreateReq groupCreateReq = new AvGroupCreateReq();
        groupCreateReq.setGroupName(groupName);
        groupCreateReq.setUserIdSet(userSet);
        RpcAvService rpcAvService = rpcClientBootstrap.getRpcConfig().getClientBean(RpcAvService.class);
        String groupCode = rpcAvService.createGroup(groupCreateReq);
        return groupCode;
    }

    public void exitGroup(String groupId) throws RpcException {
        AvGroupExitReq groupExitReq = new AvGroupExitReq();
        groupExitReq.setGroupId(groupId);
        groupExitReq.setUserIdSet(new HashSet());
        RpcAvService rpcAvService = rpcClientBootstrap.getRpcConfig().getClientBean(RpcAvService.class);
        rpcAvService.exitGroup(groupExitReq);
    }

    public Set<ChatUserListItem.UserIdTerminalType> addGroup(String groupId, Set<String> userIds) throws RpcException {
        AvGroupAddReq groupAddReq = new AvGroupAddReq();
        groupAddReq.setGroupId(groupId);
        groupAddReq.setUserIdSet(userIds);
        RpcAvService rpcAvService = rpcClientBootstrap.getRpcConfig().getClientBean(RpcAvService.class);
        ChatUserListItem chatUserListItem = rpcAvService.addGroup(groupAddReq);
        return chatUserListItem.getChatUserIdSet();
    }

    public Set<ChatUserListItem.UserIdTerminalType> sendAudioData(String groupCode, byte[] data, int length) throws RpcException {
        return this.sendData(groupCode, data, length, ByteType.audio);
    }

    public Set<ChatUserListItem.UserIdTerminalType> sendVideoData(String groupCode, byte[] data, int length) throws RpcException {
        return this.sendData(groupCode, data, length, ByteType.av);
    }

    private Set<ChatUserListItem.UserIdTerminalType> sendData(String groupCode, byte[] data, int length, ByteType byteType) throws RpcException {
        CacheService cacheService = RpcUtil.getRpcBootstrap().getRpcConfig().getCacheService();
        String userId = (String) cacheService.getCache(ImConfig.LOGIN_USER_ID);
        ChatDataReq chatDataReq = new ChatDataReq();
        chatDataReq.setGroupCode(groupCode);
        chatDataReq.setFromUserId(userId);
        chatDataReq.setByteType(byteType);
        chatDataReq.setData(data);
        chatDataReq.setLength(length);
        RpcAvService rpcAvService = rpcClientBootstrap.getRpcConfig().getClientBean(RpcAvService.class);
        ChatUserListItem chatUserListItem = rpcAvService.avChat(chatDataReq);
        return chatUserListItem.getChatUserIdSet();
    }

    public Set<ChatUserListItem.UserIdTerminalType> sendData(String groupCode, byte[] audioData, byte[] videoData) throws RpcException {
        CacheService cacheService = RpcUtil.getRpcBootstrap().getRpcConfig().getCacheService();
        String userId = (String) cacheService.getCache(ImConfig.LOGIN_USER_ID);
        AvData avData = new AvData();
        avData.setGroupCode(groupCode);
        avData.setFromUserId(userId);
        avData.setVideoData(videoData);
        avData.setVideoLength(videoData.length);
        avData.setAudioData(audioData);
        avData.setAudioLength(audioData.length);
        RpcAvService rpcAvService = rpcClientBootstrap.getRpcConfig().getClientBean(RpcAvService.class);
        ChatUserListItem chatUserListItem = rpcAvService.sendAvData(avData);
        return chatUserListItem.getChatUserIdSet();
    }
}
